package com.aiyige.page.my.settings;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.vondear.rxtools.RxFileTool;

import java.io.File;
import java.math.BigDecimal;

/**
 * 缓存工具
 * 
 * @author dance
 * 
 */
public class CacheUtils {
	
	/**
	 * 清理缓存
	 * @param context
	 */
	public static void clearCache(Context context) {
		Glide.get(context).clearDiskCache();
	}
	/**
	 * 格式化缓存大小
	 *
	 * @param size
	 * @return
	 */
	public static String getFormatSize(int scale, double size) {
		double kiloByte = size / 1024;
		if (kiloByte < 1) {
			return scale == 2 ? "0.00M" : "0.0M";
		}

		double megaByte = kiloByte / 1024;
		if (megaByte < 1) {
			BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
			return result1.setScale(scale, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + "KB";
		}

		double gigaByte = megaByte / 1024;
		if (gigaByte < 1) {
			BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
			return result2.setScale(scale, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + "M";
		}

		double teraBytes = gigaByte / 1024;
		if (teraBytes < 1) {
			BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
			return result3.setScale(scale, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + "G";
		}
		BigDecimal result4 = new BigDecimal(teraBytes);
		return result4.setScale(scale, BigDecimal.ROUND_HALF_UP).toPlainString()
				+ "T";
	}

	/**
	 * 获取缓存大小
	 * @param context
	 * @return
	 */
	public static String getCacheSize(Context context) {
		long size = 0;
		size = RxFileTool.getFileAllSize(new File( context.getExternalCacheDir(), "AiYiGeGlideCache").getAbsolutePath());
		return getFormatSize(2, size);
	}
	

}
